package com.ysu.demo.controller;


import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.goodpeople.GoodPeopleFrom;
import com.ysu.demo.from.goodpeople.GoodPeoplePages;
import com.ysu.demo.service.IGoodPeopleService;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/goodpeople")
public class GoodPeopleController {
    @Autowired
    IGoodPeopleService goodPeopleService;

    @PostMapping("/create")
    public ResultVO create(@RequestBody @Valid GoodPeopleFrom from,
                           BindingResult bindingResult){

        if(bindingResult.hasErrors() || from.getId() != null){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        goodPeopleService.c(from);

        return ResultUtil.TrueResult();
    }

    @PostMapping("/read")
    public ResultVO read(@RequestBody GoodPeoplePages pages){
        return ResultUtil.TrueResult(goodPeopleService.r());
    }


    @PostMapping("/update")
    public ResultVO update(@RequestBody @Valid GoodPeopleFrom from,
                           BindingResult bindingResult){

        if(bindingResult.hasErrors() || from.getId() == null){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        goodPeopleService.u(from);

        return ResultUtil.TrueResult();
    }


    @PostMapping("/delete")
    public ResultVO create(@RequestBody GoodPeoplePages pages){

        if(pages.getId() == null){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        goodPeopleService.d(pages.getId());

        return ResultUtil.TrueResult();
    }


}
